package com.game.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.game.pojo.vo.Result;
import com.game.pojo.po.UserPermission;
import com.game.service.UserPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户-权限 前端控制器
 * </p>
 *
 * @author lvhao
 * @since 2020-08-05
 */
@CrossOrigin
@RestController
//@RequestMapping("/userPermission")
@RequestMapping("/api")
@Api(tags = "用户权限")
public class UserPermissionController {
    @Autowired
    private UserPermissionService userPermissionService;

    @GetMapping("/userPermission/user/{id}")
    @ApiOperation("根据用户id获取权限列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "未知错误")
    })
    public Result getAll(@PathVariable int id){
        try{
            QueryWrapper<UserPermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",id);
            return new Result(200,"查询成功",userPermissionService.list(queryWrapper));
        }catch (Exception e){
            return new Result(500,"未知错误","服务器发生错误");
        }
    }

    @GetMapping("/userPermission/{id}")
    @ApiOperation("根据id查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result getById(@PathVariable("id") int id){
        try{
            if(userPermissionService.getById(id) == null){
                return new Result(202,"查询失败","记录不存在");
            }else{
                return new Result(200,"查询成功",userPermissionService.getById(id));
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @PostMapping("/userPermission")
    @ApiOperation("新建")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新建成功"),
            @ApiResponse(code = 202, message = "新建失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result add(UserPermission userPermission){
        try{
            if (userPermissionService.save(userPermission)) {
                return new Result(200,"新建成功",userPermission);
            }else{
                return new Result(202,"新建失败",userPermission);
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数有误");
        }
    }

    @PutMapping("/userPermission")
    @ApiOperation("修改")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 202, message = "修改失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result update(UserPermission userPermission){
        try{
            if (userPermissionService.updateById(userPermission)) {
                return new Result(200,"修改成功",userPermission);
            }else{
                return new Result(202,"修改失败",userPermission);
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数有误");
        }
    }

    @DeleteMapping("/userPermission/{id}")
    @ApiOperation("根据id删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 202, message = "删除失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result deleteById(@PathVariable int id){
        try{
            if(userPermissionService.removeById(id)){
                return new Result(200,"删除成功",id);
            }else {
                return new Result(202,"删除失败","id不存在");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }
}

